package com.example.taobaounion.view.fragment;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.manager.PresenterManager;
import com.example.taobaounion.model.domain.SelectContent;
import com.example.taobaounion.model.domain.SelectedPageCategory;
import com.example.taobaounion.presenter.impl.SelectPagePresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.SizeUtils;
import com.example.taobaounion.utils.TicketUtil;
import com.example.taobaounion.view.adapter.SelectedPageContentAdapter;
import com.example.taobaounion.view.adapter.SelectedPageLeftAdapter;
import com.example.taobaounion.view.callback.ISelectedPageCallback;

import java.util.Objects;

import butterknife.BindView;

public class SelectedFragment extends BaseFragment {


    public static final String TAG = "SelectedFragment";

    @BindView(R.id.left_category_list)
    public RecyclerView mLeftSelectCategories;

    @BindView(R.id.content_list)
    public RecyclerView mContentList;


    private SelectPagePresenter mSelectPagePresenter;
    private ISelectedPageCallback mISelectedPageCallback;
    private SelectedPageLeftAdapter mSelectedPageLeftAdapter;
    private SelectedPageContentAdapter mSelectedPageContentAdapter;

    @Override
    protected void initView(View view) {

        mContentList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(Objects.requireNonNull(getContext()), 4);
                outRect.top = topAndBottom;
                outRect.bottom = topAndBottom;

                int leftAndRight = SizeUtils.dip2px(Objects.requireNonNull(getContext()), 6);
                outRect.left = leftAndRight;
                outRect.right = leftAndRight;
            }
        });
    }

    @Override
    protected void initListener() {
        mSelectedPageLeftAdapter.setonLeftItemClickListener((SelectedPageCategory.DataBean dataBean) -> {

            LogUtils.d(TAG, "categories -- >" + dataBean.toString());

            mSelectPagePresenter.getContentByCategory(dataBean);
        });


        mSelectedPageContentAdapter.setOnSelectedPageContentItemClickListener(dataBean -> TicketUtil.toTicketPage(getContext(), dataBean));
    }

    @Override
    protected void initAdapter() {
        mSelectedPageLeftAdapter = new SelectedPageLeftAdapter();
        mLeftSelectCategories.setLayoutManager(new LinearLayoutManager(getContext()));
        mLeftSelectCategories.setAdapter(mSelectedPageLeftAdapter);

        mSelectedPageContentAdapter = new SelectedPageContentAdapter();
        mContentList.setLayoutManager(new LinearLayoutManager(getContext()));
        mContentList.setAdapter(mSelectedPageContentAdapter);
    }

    @Override
    protected void initPresenter() {
        mSelectPagePresenter = PresenterManager.getInstance().getSelectPagePresenter();

        mISelectedPageCallback = new ISelectedPageCallback() {
            @Override
            public void onNetworkError() {
                setUpState(STATE.ERROR);
            }

            @Override
            public void onLoading() {
                setUpState(STATE.LOADING);
            }

            @Override
            public void onEmpty() {
                setUpState(STATE.EMPTY);
            }

            @Override
            public void onCategoiesLoaded(SelectedPageCategory categories) {
                setUpState(STATE.SUCCESS);
                mSelectedPageLeftAdapter.setData(categories);
            }

            @Override
            public void onContentLoaded(SelectContent content) {
                mSelectedPageContentAdapter.setData(content);
                mContentList.scrollToPosition(0);
            }
        };
        mSelectPagePresenter.registerViewCallback(mISelectedPageCallback);
    }

    @Override
    protected void initData() {
        mSelectPagePresenter.getCategories();
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }

    @Override
    protected void onRetryClick() {
        if (mSelectPagePresenter != null) {
            mSelectPagePresenter.reloadContent();
        }
    }

    @Override
    protected void release() {
        if (mSelectPagePresenter != null) {
            mSelectPagePresenter.unRegisterViewCallback(mISelectedPageCallback);
        }
    }
}
